package platform.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.IOException;

public class Configuration {
    private static final Log logger = LogFactory.getLog(Configuration.class);
    private static CtxServerConfig ctxServerConfig;
    private static TCPConfig tcpConfig;

    public static CtxServerConfig getCtxServerConfig() {
        return ctxServerConfig;
    }

    public static TCPConfig getTcpConfig() {
        return tcpConfig;
    }

    public static void analyzer(String configuration){
        File file = new File(configuration);
        try {
            String str = FileUtils.readFileToString(file,"UTF-8");
            JSONObject obj = JSON.parseObject(str);
            JSONObject ctxObj = (JSONObject) obj.get("CtxServerConfiguration");
            JSONObject tcpObj = (JSONObject) obj.get("TCPConfig");
            ctxServerConfig  = CtxServerConfig.getInstance(ctxObj);
            tcpConfig = new TCPConfig(tcpObj);
            logger.info(ctxServerConfig);
            logger.info(tcpConfig);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
